// JavaScript Document

/*var player1, player2, player3, player4,
	yt_ready = 0;*/

// fade
$("#bg_slider").imagesLoaded({progress: function (isBroken, $images, $proper, $broken) {
	$(this).fadeTo("medium", 1);
}, always: function($images) {
	$("#bg_slider").css("background", "none");
}});

$("#wrapper").fadeIn();
$(window).on({
	resize:function() {
		//centrar contenido
		var margin = (481 - $(window).height() + 65) * -0.5;		
		if($("#content").css("display") == "block") {
			margin += 50;
			var left = 960 + ((($(window).width() - 960) * 0.5) - 184);
			if(left > 946) left = 946;
			$("#header").css("left", left);
		}
		$("#wrapper").css("margin-top", margin);	
		
		//acomodar fondo
		var height = $(window).width() * 0.529;
		margin = (height - $(window).height()) * - 0.5;
		if(margin < 0)
			$("#bg_slider img").css({"margin-top":margin, "width":"100%", "height":"auto"});
		else
			$("#bg_slider img").css({"margin-top": "", "width":"auto", "height":"100%"});
	},
	load:function() {
		$(this).trigger("resize");
	}
}).triggerHandler("resize");
// tabs navegacion
$("ul.tabs").tabs("#content > div.pane", {
	effect: "fade",
	fadeInSpeed: 600,
	initialIndex: null,
	onClick: function() {
		//stopVideo();
		if($("#content").css("display") == "block") return;
		$("#footer").hide();
		$("#wrapper").hide().children("#content").show().parent().addClass("active").fadeIn();
		$(window).trigger("resize");
	}
});
// scrollable ganadores
$("#content div.pane").eq(1).find(".scrollable").scrollable({items:".containers"});
// tabs ganadores
$("ul.mes").tabs("#content .scroll_pane", {
	tabs: "li.enabled",
	effect: "fade",
	fadeInSpeed: 600,
	initialIndex: 8,
	onBeforeClick: function() {
		$(".scrollable").each(function() {
			$(this).data("scrollable").begin();
		});
	}
});
// cerrar contenido
$("#content").on({
	click:function() {
		$("#wrapper").hide().children("#content").hide().siblings("#header").css("left", "").parent().removeClass("active").fadeIn(function() {
			$("#footer").fadeIn();
		});
		$(window).trigger("resize");
	}
}, "div.mask");
// boton footer
$("a.ver_listado").click(function() {
	$("div.pane").eq(1).children().toggle();
	if($(this).parents("#footer").length) {
		$("div.pane").eq(1).children().hide().eq(1).show();
		$("ul.tabs").children().eq(1).trigger("click");
	}
});
// scrollable videos
$("#main").scrollable({vertical: true, keyboard: 'static'}).navigator("#main_navi");
// boton compartir
$("#main_navi li").click(function(){
	//stopVideo();
	var url = [
	"http://www.youtube.com/watch?v=H9TrJW4UUdQ",
	"http://www.youtube.com/watch?v=6mEl6VO3Pj0",
	"http://www.youtube.com/watch?v=kcom9kKBsiA"]
	$("div.share").children("a").attr("href", url[$(this).index()])
});
// slideshow background
(function slideShow() {
	$("#bg_slider").find("img.current").fadeIn(2000).siblings().fadeOut(2000);
	setTimeout(function() {
		$current = $("#bg_slider").find("img.current").next();
		if(!$current.length) $current = $("#bg_slider").find("img").eq(0);
		$("#bg_slider").find("img").removeClass("current");
		$current.addClass("current");
		slideShow();
	}, 8000);
})();
// videos youtube
(function() {
	$("#player1").attr("src", "http://www.youtube.com/embed/H9TrJW4UUdQ?loop=1&wmode=transparent");
	$("#player2").attr("src", "http://www.youtube.com/embed/6mEl6VO3Pj0?loop=1&wmode=transparent");
	$("#player3").attr("src", "http://www.youtube.com/embed/kcom9kKBsiA?loop=1&wmode=transparent");
	$("#player4").attr("src", "http://www.youtube.com/embed/H9TrJW4UUdQ?loop=1&showinfo=0");
})();


/*function onYouTubeIframeAPIReady()
{
	player1 = new YT.Player("player1", {events: {'onReady': onPlayerReady}});
	player2 = new YT.Player("player2", {events: {'onReady': onPlayerReady}});
	player3 = new YT.Player("player3", {events: {'onReady': onPlayerReady}});
	player4 = new YT.Player("player4", {events: {'onReady': onPlayerReady}});
}

function onPlayerReady() {
	yt_ready++;
}

function stopVideo() {
	if(yt_ready < 3) return;
	if(player1) player1.stopVideo();
	if(player2) player2.stopVideo();
	if(player3) player3.stopVideo();
	if(player4) player4.stopVideo();
}*/